package com.xiyu.service.service.elder;

import com.xiyu.service.model.elder.register.ElderTryLiving;
import com.xiyu.service.model.elder.register.ElderTryLivingDraft;
import com.xiyu.service.repository.elder.register.ElderTryLivingRepository;
import com.xiyu.service.service.infra.data.DictNoService;
import com.xiyu.service.vo.elder.tryLiving.TryLivingGetOutput;
import com.xiyu.service.vo.elder.tryLiving.TryLivingQueryOutput;
import com.xiyu.service.vo.elder.tryLiving.TryLivingQueryInput;
import com.xiyu.service.vo.elder.tryLiving.TryLivingUpdateInput;
import com.xiyu.service.vo.elder.tryLiving.TryLivingCreateInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.elder.TryLivingConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.elder.TryLivingErrorCode.*;

/**
 * 试住登记 Service 实现类
 */
@Service
@Validated
public class TryLivingServiceImpl implements TryLivingService {

    @Resource
    private ElderTryLivingRepository elderTryLivingRepository;

    @Resource
    private DictNoService dictNoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(TryLivingCreateInput inputVO) {
        ElderTryLiving newTry = TryLivingConvert.INSTANCE.createInputConvert(inputVO);
        if (inputVO.getBeginTime().isAfter(inputVO.getEndTime()))
            throw exception(ELDER_TRY_LIVING_TIME_ERROR);
        newTry = ElderTryLivingDraft.$.produce(newTry, draft -> {
            draft.setTryNo(dictNoService.produceNo("tryLiving"));
        });
        newTry = elderTryLivingRepository.insert(newTry);
        return newTry.id();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(TryLivingUpdateInput inputVO) {
        ElderTryLiving updateTry = TryLivingConvert.INSTANCE.updateInputConvert(inputVO);
        Optional<ElderTryLiving> opOldTry = elderTryLivingRepository.findById(updateTry.id());
        if(!opOldTry.isPresent())
            throw exception(ELDER_TRY_LIVING_EXISTS);
        if (inputVO.getBeginTime().isAfter(inputVO.getEndTime()))
            throw exception(ELDER_TRY_LIVING_TIME_ERROR);
        elderTryLivingRepository.update(updateTry);
        return true;
    }

    @Override
    public PageResult<TryLivingQueryOutput> query(TryLivingQueryInput inputVO) {
        Page<ElderTryLiving> tryLivingPage = elderTryLivingRepository.pageQuery(inputVO);
        List<TryLivingQueryOutput> tryLivingQueryOutputs = TryLivingConvert.INSTANCE.queryPageOutputConvert(tryLivingPage.getRows());
        return new PageResult<>(tryLivingQueryOutputs, tryLivingPage.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleted(Long id) {
        Optional<ElderTryLiving> opDeleteTry = elderTryLivingRepository.findById(id);
        if(!opDeleteTry.isPresent())
            throw exception(ELDER_TRY_LIVING_EXISTS);
        elderTryLivingRepository.deleteById(id);
        return true;
    }

    @Override
    public TryLivingGetOutput get(Long id) {
        Optional<ElderTryLiving> opTry = elderTryLivingRepository.getById(id);
        if(!opTry.isPresent())
            throw exception(ELDER_TRY_LIVING_EXISTS);
        return TryLivingConvert.INSTANCE.getOutputConvert(opTry.get());
    }

}
